package cn.djun.news.controller;

import cn.djun.news.entity.News;
import cn.djun.news.service.NewsService;
import cn.djun.news.util.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author djun
 * @classname NewsController
 * @description
 * @date 2020/11/24 14:38
 * @email 3170495286@qq.com
 */
@RestController
@RequestMapping("/news")
@Slf4j
@Api(tags = "新闻管理API")
public class NewsController {

    @Autowired
    private NewsService newsService;

    /**
     * 新增新闻
     *
     * @param news
     * @return
     */
    @PutMapping("/add")
    @PassToken
    @ApiOperation(value = "新增新闻", notes = "新增新闻", httpMethod = "PUT")
    public ResponseEntity<BaseResponse> addNews(@RequestBody News news) {
        try {
            //判断用户名是否重复
            if (newsService.isNewsName(news.getTitle())) {
                return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.USER_REPETITION_NEWS, "");
            }
            //调用service层方法
            int flag = newsService.addNews(news);
            if (flag == 1) {
                return BaseResponse.generateBadResponseEntity(Constant.SUCCES_CODE, Constant.INSERT_SUCCES_NEWS, "");
            } else {
                return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.INSERT_FAILD_NEWS, "");
            }
        } catch (Exception e) {
            return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.SERVE_ERROR_MSG, "");
        }
    }

    /**
     * 分页
     *
     * @param news
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查看新闻列表", notes = "查看新闻列表，title模糊查询", httpMethod = "GET")
    public ResponseEntity<BaseResponse<Paginating<News>>> getNewslist(News news) {
        log.debug(news.toString());
        try {
            Paginating<News> paginating = newsService.getNewsList(news);
            return BaseResponse.generateBadResponseEntity(Constant.SUCCES_CODE, Constant.SELECT_SUCCES, paginating);
        } catch (Exception e) {
            return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.SERVE_ERROR_MSG, "");
        }
    }

    /**
     * 随机查询4条新闻
     *
     * @param news
     * @return
     */
    @GetMapping("/fourlist")
    @ApiOperation(value = "查看新闻列表", notes = "随机查询4条新闻", httpMethod = "GET")
    public ResponseEntity<BaseResponse<Paginating<News>>> getfourList(News news) {
        log.debug(news.toString());
        try {
            Paginating<News> paginating = newsService.getfourList(news);
            return BaseResponse.generateBadResponseEntity(Constant.SUCCES_CODE, Constant.SELECT_SUCCES, paginating);
        } catch (Exception e) {
            return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.SERVE_ERROR_MSG, "");
        }
    }

    /**
     * 修改新闻
     *
     * @param news
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改新闻", notes = "根据id修改新闻", httpMethod = "POST")
    public ResponseEntity<BaseResponse> updateUser(
            @RequestBody News news) {
        try {
            int flag = newsService.updata(news);
            if (flag == 1) {
                return BaseResponse.generateBadResponseEntity(Constant.SUCCES_CODE, Constant.UPADTA_SUCCES, "");
            } else {
                return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.UPADTA_FAILD, "");
            }
        } catch (Exception e) {
            return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.SERVE_ERROR_MSG, "");
        }
    }

    /**
     * 根据id删除新闻
     *
     * @param request
     * @param news
     * @return
     */
    @DeleteMapping("/del")
    @ApiOperation(value = "删除新闻", notes = "根据id删除新闻", httpMethod = "DELETE")
    public ResponseEntity<BaseResponse> delUser(HttpServletRequest request, News news) {
        try {
            int flag = newsService.delNews(news.getNewsid());
            if (flag == 1) {
                return BaseResponse.generateBadResponseEntity(Constant.SUCCES_CODE, Constant.DELETE_NEWS_SUCCESS, "");
            } else {
                return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.DELETE_NEWS_FAILD, "");
            }
        } catch (Exception e) {
            return BaseResponse.generateBadResponseEntity(Constant.SERVE_ERROR, Constant.SERVE_ERROR_MSG, "");
        }
    }
}
